//
//  NSCalendar+JXCoveniences.m
//  TestCollectionView
//
//  Created by Lemon on 2019/4/20.
//  Copyright © 2019 sunshinfight@163.com. All rights reserved.
//

#import "NSCalendar+JXCoveniences.h"

#define macroCalendar [NSCalendar currentCalendar]

@implementation NSCalendar (JXCoveniences)
+ (int32_t)dayWithDate: (NSDate*)date
{
    int32_t day = (int32_t)[macroCalendar component:NSCalendarUnitDay fromDate:date];
    return day;
}

+ (int32_t)realWeekdayWithDate: (NSDate*)date
{
    
    int32_t week = [self weekdayWithDate:date];
    /**
     因为系统提供计算weekday的方法是以周日作为一周的开始，
     范围是[1, 7] <--> [周日, 周六]，转换为[1, 7] <--> [周一, 周日]
     */
    return (week - 1) == 0 ? 7 : (week - 1);
}

+ (int32_t)weekdayWithDate: (NSDate*)date
{
    macroCalendar.firstWeekday = 1;
    int32_t week = (int32_t)[macroCalendar component:NSCalendarUnitWeekday fromDate:date];
    return (int32_t)week;
}

+ (int32_t)daysInMonthWithDate: (NSDate*)date
{
    NSRange daysRangeInMonth = [macroCalendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return (int32_t)(daysRangeInMonth.length);
}

+ (int32_t)monthWithDate: (NSDate*)date
{
    int32_t month = (int32_t)[macroCalendar component:NSCalendarUnitMonth fromDate:date];
    return month;
}

+ (int32_t)yearWithDate: (NSDate*)date
{
    int32_t year = (int32_t)[macroCalendar component:NSCalendarUnitYear fromDate:date];
    return year;
}

@end
